﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;

namespace Shutterstock.Salesforce.SSISDownload.Logging
{

    public static class Logger
    {
        public static Semaphore WriterPool = new Semaphore(1, 1);
        public static SqlConnection conn;
        static int run_id;

        public static void Init(string connectionstring)
        {
            if (conn == null)
            {
                conn = new SqlConnection(connectionstring);
                conn.Open();
                CreateLog(conn);
                SqlCommand cmd = new SqlCommand("select ISNULL(MAX(run_id),0)from log", conn);
                run_id = int.Parse(cmd.ExecuteScalar().ToString()) + 1;
                cmd.Dispose();
            }
        }

        private static void CreateLog(SqlConnection conn)
        {
            string createTable = "IF object_id('Log') is null " +
                                " begin " +
                                " Create Table Log ( " +
                                "  log_id int identity(1,1) " +  
                                ", run_id	int " +
                                ", function_type varchar(60) " +
                                ", table_name varchar(60) " +
                                ", task_type varchar(60) " +
                                ", start_time datetime " +
                                ", insert_time datetime " +
                                ", count	int " +
                                ") " +
                                "end ";
            if (conn.State != System.Data.ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand CreateTableStatement = conn.CreateCommand();
            CreateTableStatement.CommandText = createTable;
            CreateTableStatement.ExecuteNonQuery();

        }


        public static void Log(FunctionType fnType, string tableName, TableTaskType type, DateTime startTime, int count)
        {
            DateTime now = DateTime.Now;
            WriterPool.WaitOne();
            SqlCommand cmd = conn.CreateCommand();
            try
            {
                if (conn.State != System.Data.ConnectionState.Open)
                    conn.Open();
                cmd.CommandText = "insert into log(run_id, function_type, table_name, task_type, start_time,insert_time, count) values(@runId, @fnType,@tableName,@taskType,@startTime, @insertTime, @count)";
                cmd.Parameters.AddWithValue("runId", run_id);
                cmd.Parameters.AddWithValue("fnType", fnType.ToString());
                cmd.Parameters.AddWithValue("tableName", tableName);
                cmd.Parameters.AddWithValue("taskType", type.ToString());
                cmd.Parameters.AddWithValue("startTime", startTime);
                cmd.Parameters.AddWithValue("insertTime", now);
                cmd.Parameters.AddWithValue("count", count);
                cmd.ExecuteNonQuery();
            }
            finally
            {
                cmd.Dispose();
            }
            WriterPool.Release();
        }

        public static void LogEvent(string source, string ErrorMessage, EventLogEntryType EventType)
        {
            EventLog eventLog = new EventLog();
            eventLog.Source = source;
            eventLog.WriteEntry(ErrorMessage, EventType);
            Debug.WriteLine(ErrorMessage);
        }
    }
}
 